package edu.hawaii.myisern.action;

import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import org.xml.sax.SAXException;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * ActionBean for Researcher, both listing and editing.
 * 
 * @author Shaoxuan Zhang
 * @author Laura Matsuo
 *
 */
public class ResearcherActionBean extends MyIsernActionBean {
  /** Our single researcher instance manipulated by all webapp users. */
  private Researcher researcher = new Researcher();
  /** MyIsern instance manipulated by all webapp users. */
  private MyIsernModel myIsern = MyIsernModel.getInstance();;
  private String message = "";

  /**
   * Constructor
   * 
   * @throws JAXBException when JAXB fails.
   * @throws SAXException when SAX fails.
   */
  public ResearcherActionBean() throws JAXBException, SAXException {
    // empty constructor
  }

  /**
   * Gets the message to display to the user, such as when a researcher has been added.
   * 
   * @return The message to display to the user.
   */
  public String getMessage() {
    return this.message;
  }

  /**
   * Sets the message to display to the user, such as when a researcher has been added.
   * 
   * @param message The message to display to the user.
   */
  public void setMessage(String message) {
    this.message = message;
  }

  /**
   * Sets the researcher's name.
   * 
   * @param name The researcher's name.
   */
  public void setName(String name) {
    this.researcher.setName(name);
  }

  /**
   * Sets the researcher's email.
   * 
   * @param email The researcher's email.
   */
  public void setEmail(String email) {
    this.researcher.setEmail(email);
  }

  /**
   * Sets the researcher's organization.
   * 
   * @param organization The researcher's organization.
   */
  public void setOrganization(String organization) {
    this.researcher.setOrganization(organization);
  }

  /**
   * Sets the researcher's picture link.
   * 
   * @param pictureLink The researcher's picture link.
   */
  public void setPictureLink(String pictureLink) {
    this.researcher.setPictureLink(pictureLink);
  }

  /**
   * Sets the researcher's bio statement.
   * 
   * @param bioStatement The researcher's bio statement.
   */
  public void setBioStatement(String bioStatement) {
    this.researcher.setBioStatement(bioStatement);
  }

  /**
   * Gets the researcher's name.
   * 
   * @return The researcher's name.
   */
  public String getName() {
    return this.researcher.getName();
  }

  /**
   * Gets the researcher's email.
   * 
   * @return The researcher's email.
   */
  public String getEmail() {
    return this.researcher.getEmail();
  }

  /**
   * Gets the researcher's organization.
   * 
   * @return The researcher's organization.
   */
  public String getOrganization() {
    return this.researcher.getOrganization();
  }

  /**
   * Gets the researcher's picture link.
   * 
   * @return The researcher's picture link.
   */
  public String getPictureLink() {
    return this.researcher.getPictureLink();
  }

  /**
   * Gets the researcher's bio statement.
   * 
   * @return The researcher's bio statement.
   */
  public String getBioStatement() {
    return this.researcher.getBioStatement();
  }

  /**
   * Provides an iterator over the researchers to the page.
   * 
   * @return The researchers iterator.
   */
  public Iterator<Researcher> getResearchersIterator() {
    return this.myIsern.getResearcherList().iterator();
  }

  /**
   * Return a list of invalid referencer of organiztion within this researcher.
   * @return a List of String.
   */
  public List<String> getInvalidReferenceInResearcher() {
    return this.myIsern.getInvalidReference(this.researcher);
  }
  
  /**
   * Updates the researcher's information if the researcher already exists in the list, otherwise
   * adds the researcher to the list.
   * 
   * @return A Resolution indicating to display the researcherDisplayMessage.jsp page.
   */
  public Resolution save() {
    if (this.researcher.getName() == null || this.researcher.getName().length() == 0) {
      this.message = "Nothing is added.";
    }
    //Check if there is any invalid information
    else if (this.myIsern.semanticCheck(this.researcher)) {
      //See if it is a new researcher.
      if (this.myIsern.add(researcher)) {
        this.message = " Thank you for entering Researcher " + researcher.getName();
      }
      else {
        this.message = "Researcher " + researcher.getName() + " has been updated.";
      }
    }
    else {
      //if has invalid information
      this.myIsern.add(this.researcher);
      this.message = "There are invalid information within your input.";
      return new ForwardResolution("/researcherError.jsp");
    }
    return new ForwardResolution("/researchersList.jsp");
  }

  /**
   * Finds the researcher to edit if the researcher already exists.
   * 
   * @return A Resolution indicating to display the researcherEdit.jsp page.
   */
  public Resolution edit() {
    this.researcher = this.myIsern.getResearcher(this.getName());
    if (this.researcher == null) {
      this.researcher = new Researcher();
    }
    return new ForwardResolution("/researcherEdit.jsp");
  }

  /**
   * Finds the researcher to display.
   * 
   * @return A Resolution indicating to display the researcherView.jsp page.
   */
  public Resolution view() {
    if (this.myIsern.hasResearcher(this.getName())) {
      this.researcher = this.myIsern.getResearcher(this.getName());
      return new ForwardResolution("/researcherView.jsp");
    }
    else {
      return new ForwardResolution("/researcherNotExist.jsp");
    }
  }

  /**
   * Deletes the researcher specified by name.
   * 
   * @return A Resolution indicating to display the researcherDisplayMessage.jsp page.
   */
  public Resolution delete() {
    if (this.myIsern.remove(researcher.getName())) {
      this.message = "Researcher: " + this.getName() + "was successfully deleted";
    }
    else {
      this.message = "Researcher: " + this.getName() + "not found. Maybe delete by other users";
    }
    return new ForwardResolution("/researchersList.jsp");
  }

  /**
   * Forwards the user to the researchersList.jsp page to view all researchers.
   * 
   * @return A Resolution indicating to redisplay the researchersList.jsp page.
   */
  @DefaultHandler
  public Resolution listResearchers() {
    return new ForwardResolution("/researchersList.jsp");
  }
}
